package hluck.me.srorage.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import hluck.me.srorage.sys.common.DataGridView;
import hluck.me.srorage.sys.common.ResultObj;
import hluck.me.srorage.sys.common.TreeNode;
import hluck.me.srorage.sys.domain.SysDept;
import hluck.me.srorage.sys.service.SysDeptService;
import hluck.me.srorage.sys.vo.DeptVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 老雷
 * @since 2021-02-15
 */
@RestController
@RequestMapping("/dept")
public class SysDeptController {

    @Autowired
    private SysDeptService deptService;

    /**
     * 加载部门管理左边的树的json
     */
    @RequestMapping("loadDeptManagerLeftTreeJson")
    public DataGridView loadDeptManagerLeftTreeJson(DeptVo deptVo){
        List<SysDept> list = this.deptService.list();

        ArrayList<TreeNode> treeNodes = new ArrayList<>();
        for (SysDept sysDept : list) {
            Boolean spread=sysDept.getOpen()==1?true:false;
            treeNodes.add(new TreeNode(sysDept.getId(),sysDept.getPid(),sysDept.getTitle(),spread));
        }
        return new DataGridView(treeNodes);
    }
    /**
     * 全查询
     */
    @RequestMapping("loadAllDept")
    public DataGridView loadAllDept(DeptVo deptVo){
        IPage<SysDept> page=new Page<>(deptVo.getPage(),deptVo.getLimit());

        QueryWrapper<SysDept> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(deptVo.getTitle()),"title",deptVo.getTitle());
        queryWrapper.like(StringUtils.isNotBlank(deptVo.getAddress()),"address",deptVo.getAddress());
        queryWrapper.like(deptVo.getRemark()!=null,"remark",deptVo.getRemark());
        queryWrapper.eq(deptVo.getId()!=null,"id",deptVo.getId()).or().eq(deptVo.getId()!=null,"pid",deptVo.getId());
        queryWrapper.orderByAsc("ordernum");

        this.deptService.page(page,queryWrapper);

        return new DataGridView(page.getTotal(),page.getRecords());
    }

    /**
     * 添加部门
     */
    @RequestMapping("addDept")
    public ResultObj addDept(DeptVo deptVo){
        try {
            deptVo.setCreatetime(new Date());
            this.deptService.save(deptVo);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }
    /**
     * 修改部门
     */
    @RequestMapping("updateDept")
    public ResultObj updateDept(DeptVo deptVo){
        try {
            this.deptService.updateById(deptVo);
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }


    /**
     * 加载最大的排序码
     */
    @RequestMapping("loadMaxDeptOrderNum")
    public Map<String,Object> loadMaxOrderNum(){
        HashMap<String, Object> map = new HashMap<>();

        QueryWrapper<SysDept> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByDesc("ordernum");
        List<SysDept> list = deptService.list(queryWrapper);
        if (list.size()>0){
            map.put("value",list.get(0).getOrdernum()+1);
        }else {
            map.put("value",1);
        }

        return map;
    }

    /**
     * 检查是否含有子节点
     */
    @RequestMapping("checkChildrenNode")
    public Map<String,Object> checkChildrenNode(DeptVo deptVo){
        HashMap<String, Object> map = new HashMap<>();

        QueryWrapper<SysDept> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("pid",deptVo.getId());
        List<SysDept> list = deptService.list(queryWrapper);
        if (list.size()>0){
            map.put("value",true);
        }else {
            map.put("value",false);
        }

        return map;
    }

    /**
     * 删除部门
     */
    @RequestMapping("deleteDept")
    public ResultObj deleteDept(DeptVo deptVo){
        try {
            this.deptService.removeById(deptVo.getId());
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

}

